﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

[assembly: TagPrefix("Jalal.Library.Web.Control", "ucc")]
namespace Jalal.Library.Web.Control
{
    [SupportsEventValidation, ValidationProperty("Text")]
    [ToolboxData("<{0}:WatermarkTextBox runat=server></{0}:WatermarkTextBox>")]
    public class WatermarkTextBox : TextBox
    {
        private string _initialText;
        public string InitialText
        {
            get { return this._initialText; }
            set { this._initialText = value; }
        }

        private bool _inWatermarkMode;
        public bool InWatermarkMode
        {
            get { return this._inWatermarkMode; }
            set { this._inWatermarkMode = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            if (InWatermarkMode && Text == InitialText)
                Style[HtmlTextWriterStyle.Color] = "#dbdada";
            else
                Style[HtmlTextWriterStyle.Color] = "#000000";

            StringBuilder script = new StringBuilder();
            script.Append("<script type=\"text/javascript\">function cleartext(){");
            script.Append(" var initialText = \"").Append(InitialText).Append("\";");
            script.Append(" var currentText = document.getElementById(\"").Append(ClientID).Append("\").value;");
            script.Append(" if(initialText == currentText){document.getElementById(\"").Append(ClientID).Append("\").value=\"\";");
            script.Append(" document.getElementById(\"").Append(ClientID).Append("\").style.color=\"#000000\";}");
            script.Append("}</script>");
            Page.ClientScript.RegisterClientScriptBlock(GetType(),"WatermarkEffect", script.ToString());
            this.Attributes.Add("onclick", "javascript:cleartext()");
            base.OnLoad(e);
        }
    }
}
